package model

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:4ay1nkal3u8ed77y@tcp(14.103.202.69:3306)/xy?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
		fmt.Println(dsn)
	}
	err = DB.AutoMigrate(&User{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据表迁移成功")
	}
}
